import java.util.Comparator;
import java.util.Objects;

public class HeapBijiao {


     static class Student implements Comparable<Student>{
        public int age;
        public String name;

        public Student(int age, String name) {
            this.age = age;
            this.name = name;
        }

        public Student() {

        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Student student = (Student) o;
            return age == student.age && Objects.equals(name, student.name);
        }

        @Override
        public int hashCode() {
            return Objects.hash(age, name);
        }

         @Override
         public int compareTo(Student o) {
             return this.age-o.age;
//           return o.age-this.age;       就变成大根堆了
         }
     }
     //自己定义名字比较器，避免上面写死情况
     static class NameComparator implements Comparator<Student>{

         @Override
         public int compare(Student o1, Student o2) {
             return o1.name.compareTo(o2.name);
         }
     }

    public static void main(String[] args) {
        Student student=new Student(10, "zhangsan");
        Student student1=new Student(10, "zhangsan");

        System.out.println(student==student1);  //比较的是两个地址
        System.out.println(student.equals(student1));//调用的是Object的equals,意义和上面一样

        //调用的equals只能判断true不能比较大小，所以要调用Comparable
        System.out.println(student.compareTo(student1));

        NameComparator nameComparator=new NameComparator();
        int ret = nameComparator.compare(student1,student);
        System.out.println(ret);

        //总结：不用Student实现Comparale方法，直接定义比较器
    }
}
